package features.advance.leetcode.math.medium;

/**
 *  剑指 Offer 64. 求1+2+…+n
 *
 *  难度：中等
 *
 * 求 1+2+...+n ，要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句（A?B:C）。
 *
 *
 *
 * 示例 1：
 *
 * 输入: n = 3
 * 输出: 6
 * 示例 2：
 *
 * 输入: n = 9
 * 输出: 45
 *
 *
 * 限制：
 *
 * 1 <= n <= 10000
 *
 * @author LIN
 * @date 2021-06-22
 */
public class Offer64 {

    public static void main(String[] args) {
        Solution solution = new Solution() {
            @Override
            public int sumNums(int n) {
                    int ans = 0, A = n, B = n + 1;
                    boolean flag;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    flag = ((B & 1) > 0) && (ans += A) > 0;
                    A <<= 1;
                    B >>= 1;

                    return ans >> 1;
                }
        };
        int n = 3;
        int res = solution.sumNums(n);
        System.out.println(res);
    }

    static class Solution {
        /**
         * 递归
         * @param n
         * @return
         */
        public int sumNums(int n) {
            boolean flag = n > 0 && (n += sumNums(n - 1)) > 0;
            return n;
        }


    }

}
